home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2659 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Problem with generic classes/templates
  5. Date: 18 Jan 1996 21:48:38 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Distribution: world
  8. Message-ID: <4dmf7m$b6a@colossus.holonet.net>
  9. References: <4dlerb$r3r@finch.doc.ic.ac.uk>
  10. NNTP-Posting-Host: jubal.mdli.com
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Benjamin Jefferys (brj@doc.ic.ac.uk) wrote:
  14. [...]
  15.  
  16. : class meringue{
  17. [OK]
  18. : };
  19.  
  20. : template <class X> class marzipan<X>{
  21.                                    ^^^  OMIT THIS
  22. :   X ian;
  23. :   public:
  24. :   marzipan<X>(/* what goes here? */) { /* and what goes here? */ }
  25.             ^^^ OMIT THIS
  26. : }
  27.  
  28. Well, it really depends on how you want to construct marzipan<X>
  29. objects.  I think here you need to concern yourself more with 
  30. marzipans, and leave any decision about X's up to the caller by
  31. letting him pass in an X as follows:
  32.   marzipan(const X& x) : ian(x) {} // inits ian with X's copy ctor
  33. and you might also want a default ctor which could simply be:
  34.   marzipan() {}                    // inits ian with X's default ctor 
  35.     
  36. : Once I've got these, I try a:
  37.  
  38. : marzipan<meringue> bob;
  39.  
  40. This would call the default ctor, and bob.ian would be a default
  41. meringue.   Alternatively, you could construct a meringue object
  42. as you like, and then invoke the marzipan(const X&) ctor, e.g.
  43.  
  44.    meringue m( whatever ); 
  45.    marzipan<meringue> bob( m );  // bob.ian is a copy of m
  46.  
  47. This is the only approach that makes much sense to me.  You could
  48. of course make additional marzipan ctors, that take other arguments
  49. which you pass on to the X ctor, but that would lose the generality
  50. of your template.  For example, you could have
  51.    marzipan( float a, float b ) : X(a,b) {}
  52. but this would fail to compile for many X's that are not meringues.
  53.  
  54. [...]
  55.  
  56. : (As a matter of interest, the generic class(es) implements a doubly-linked
  57. : list, which obviously has to store any class to be useful)
  58.  
  59. Hmm, linked lists are usually done with pointers, not contained
  60. classes.  I'm not sure where your marzipan template fits into your
  61. design, but I sense trouble! 
  62. --
  63. Russell Blackadar,   russell@mdli.com
  64.